package com.bilibili.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bilibili.entity.CommonResult;
import com.bilibili.service.VideoService;
import com.bilibili.service.impl.VideoServiceImpl;

@SuppressWarnings("serial")
@WebServlet("/updateSomeVideoState")
public class UpdateSomeVideoStateServlet extends HttpServlet{
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("application/json; charset=utf-8");
		String[] videoIds = request.getParameterValues("videoIds");
		String state = request.getParameter("state");
		int status = -1;
		if(state != null) {
			status = Integer.parseInt(state);
		}
		VideoService videoService = new VideoServiceImpl();
		CommonResult cr = new CommonResult();
		int num = 0;
		for(int i = 0;i<videoIds.length;i++) {
			if(videoService.updateVideoStateByVideoId(status,Integer.parseInt(videoIds[i]))) {
				num++;
			}
		}
		if(num == videoIds.length) {
			cr.setCode(200);
		}else {
			cr.setCode(409);
		}
		response.getWriter().write(cr.toJSONString());
	}
}
